package net.sunbeta.test.util;

import java.io.IOException;
import java.io.StringReader;
import java.util.List;

import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;

public class XPathHelper {
    public Document readDocument(String text) {
        try {
            return new SAXBuilder().build(new StringReader(text));
        } catch (JDOMException e) {
            throw new IllegalArgumentException("Invalid xml", e);
        } catch (IOException e) {
            throw new IllegalArgumentException("Invalid xml", e);
        }
    }

    // assert:
    //Document doc = readDocument(result);
    //assertValid(doc, "//helloWorldResponse[text()='Hello World']");
    @SuppressWarnings("rawtypes")
    public static void assertValid(Document doc, String selector) throws JDOMException {
        XPath xpath = XPath.newInstance(selector);
        List nodes = xpath.selectNodes(doc);
        assert nodes.size() > 0 : "No nodes matching xpath " + selector;
    }
}
